package com.ie.service.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ie.common.http.ResponseResult;
import com.ie.entity.GoodsEntity;
import com.ie.mapper.admin.GoodsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, GoodsEntity> implements GoodsService {
    @Autowired
    private GoodsMapper goodsMapper;

    @Override
    public ResponseResult<Map<String, Object>> getGoods(GoodsEntity goodsEntity) {
        Integer pagenum = goodsEntity.getCurrentPage();
        if (pagenum == null || pagenum < 1) {
            goodsEntity.setCurrentPage(1);
        }
        IPage<GoodsEntity> iPage = new Page<>(goodsEntity.getCurrentPage(), 5);
        LambdaQueryWrapper<GoodsEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        Integer goodstypeId = goodsEntity.getGoodstypeId();
        String gname = goodsEntity.getGname();
        if (goodstypeId != null && goodstypeId != 0) {
            lambdaQueryWrapper.eq(GoodsEntity::getGoodstypeId, goodstypeId);
        }
        if (gname != null && !gname.isEmpty()) {
            lambdaQueryWrapper.like(GoodsEntity::getGname, gname);
        }
        IPage<GoodsEntity> Page = goodsMapper.selectPage(iPage, lambdaQueryWrapper);
        HashMap<String, Object> myres = new HashMap<>();
        myres.put("allGoods", Page.getRecords());
        myres.put("totalPage", Page.getPages());
        return ResponseResult.getSuccessResult(myres);
    }
}

